(define tolerance 0.00001)
(define (fixed-point f first-guess)
    (define (close-enough? v1 v2)
        (< (abs (- v1 v2)) tolerance)
    )
    (define (try guess)
        (let ((next (f guess)))
            (if (close-enough? guess next)
                next
                (try next)
            )
        )
    )
    (try first-guess)
)
(define dx tolerance)
(define (deriv g)
    (lambda (x)
        (/ (- (g (+ x dx)))
            dx 
        )
    )
)
(define (newton-transform g)
    (lambda (x) 
        (- x (/ (g x) ((deriv g) x)))
    )
)
(define (newtons-method g guess)
    (fixed-point (newton-transform g) guess)
)
(define (sqrt x)
    (newtons-method 
        (lambda (y)
            (- (* y y) x) 
        )
        1.0
    )    
)
(display (sqrt 2))
(newline)

(define (cubic a b c) 
    (newtons-method
        (lambda (y) 
            (+
                (* y y y)
                (* a y y)
                (* b y)
                c
            )
        )
        1.0
    )
)
(display (cubic 1 1  1))
(exit)